<!--
 * @Author: ZhaoZhiqi
 * @Date: 2022-08-09 13:39:39
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2022-12-13 13:09:06
 * @Description: 12345热线
 * @FilePath: \tuanfeng-admin-front\src\pages\Question\index.vue
-->
<script setup lang="ts">
import moment from "moment";
import { Search, Plus } from "@element-plus/icons-vue";
import SearchItem from "../../components/SearchItem.vue";
import TableCell from "../../components/TableCell.vue";
import EditDrawer from "../../components/EditDrawer.vue";
import Editor from "../../components/Editor.vue";
import LogDialog from "../../components/LogDialog.vue";
import { ElMessage } from "element-plus";
import {
    exportExcelAPI,
    updateOpenStatusAPI,
    deleteQuestionAPI
} from "../../api/question";
import { ref, reactive } from "vue";
import useTable from "./useTable";
import useForm from "./useForm";
import { useRoute } from "vue-router";
import { useRoleStore } from "../../store/role.js";

const path = useRoute().path;
const permissions = useRoleStore().permissions;
const permission = permissions[path] || {};
console.log('permission', permission)

const logVisible = ref(false);
// 查询条件
let filters = reactive({
    id: "",
    title: "",
    content: "",
    probTypeCode: "",
    replyStatus: "",
    acceptDeptId: "",
    pageNo: 1,
    pageSize: 10,
});

// 列表数据
const { tableData, loading, totalCount, createTime, questionTypes, query } =
    useTable(filters);

// 编辑表单
const { dialogVisible, formRef, isEdit, form, deptsList, handleEdit, handleConfirm } =
    useForm(query);

// 导出 
const handleExport = () => {
    exportExcelAPI({
        ids: tableData.value.map(item => item.id)
    }).then(res => {

    })
}

// 公开
const handleOpen = (row) => {
    let { id, isOpen } = row;
    updateOpenStatusAPI({
        id,
        isOpen: !isOpen
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "更新成功",
                type: "success",
            });
            query();
        }
    });
};

// 删除
const handleDelete = (id: string) => {
    deleteQuestionAPI({
        id,
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "删除成功",
                type: "success",
            });
            query();
        }
    });
};

const rules = reactive({
    acceptDeptName: [{ required: true, message: "请输入受理部门", trigger: "blur" }],
    reply: [{ required: true, message: "请输入回复内容", trigger: "blur" }],
});
</script>

<template>
    <div>
        <el-card class="search-wrapper common-card">
            <el-row :gutter="24">
                <SearchItem label="问题id">
                    <el-input clearable style="width: 100%" placeholder="请输入问题id" v-model="filters.id"></el-input>
                </SearchItem>
                <SearchItem label="问题标题">
                    <el-input clearable style="width: 100%" placeholder="请输入问题标题" v-model="filters.title"></el-input>
                </SearchItem>
                <SearchItem label="问题内容">
                    <el-input clearable style="width: 100%" placeholder="请输入问题内容" v-model="filters.content"></el-input>
                </SearchItem>
                <SearchItem label="问题类型">
                    <el-select style="width: 100%" v-model="filters.probTypeCode">
                        <el-option label="全部" value></el-option>
                        <el-option v-for="item in questionTypes" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                </SearchItem>
                <SearchItem label="问题状态">
                    <el-select style="width: 100%" v-model="filters.replyStatus">
                        <el-option label="全部" value></el-option>
                        <el-option label="待回答" value="0"></el-option>
                        <el-option label="已回答" value="1"></el-option>
                    </el-select>
                </SearchItem>
                <SearchItem label="受理部门">
                    <el-input clearable style="width: 100%" placeholder="请输入受理部门" v-model="filters.acceptDeptName"></el-input>
                </SearchItem>
                <SearchItem label="创建时间">
                    <el-date-picker v-model="createTime" value-format="x" style="width: 100%; box-sizing: border-box" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
                </SearchItem>
            </el-row>

            <div class="search-button-wrapper">
                <!-- <el-button :icon="Plus" plain auto-insert-space @click="() => handleEdit(false)">新增</el-button> -->
                <el-button v-if="permission['导出']" :icon="Plus" plain auto-insert-space @click="() => handleExport()">导出</el-button>
                <el-button type="primary" :icon="Search" auto-insert-space @click="query">查询</el-button>
            </div>
        </el-card>

        <!-- Table -->
        <el-card class="common-card" style="width: 100%">
            <!-- 列表 -->
            <el-table v-loading="loading" :data="tableData" stripe width="1500px">
                <el-table-column prop="id" label="id" width="80" />
                <el-table-column prop="title" label="问题标题" width="200">
                    <template #default="scope">
                        <TableCell :content="scope.row.title"></TableCell>
                    </template>
                </el-table-column>
                <el-table-column prop="content" label="问题描述" width="200">
                    <template #default="scope">
                        <TableCell :content="scope.row.content"></TableCell>
                    </template>
                </el-table-column>
                <el-table-column prop="reply" label="回复内容" width="200">
                    <template #default="scope">
                        <TableCell :content="scope.row.reply"></TableCell>
                    </template>
                </el-table-column>
                <el-table-column prop="probTypeName" label="问题类型"  width="100"/>
                <el-table-column prop="createBy" label="提问人姓名" width="150" />
                <el-table-column prop="phone" label="提问人电话" width="150" />
                <el-table-column prop="mail" label="提问人邮箱" width="180" />
                <el-table-column prop="isOpen" label="是否公开" width="80">
                    <template #default="scope">
                        <div>{{scope.row.isOpen ? '是' : '否'}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="fileList" label="附件" width="300">
                    <template #default="scope">
                        <el-link v-for="item in scope.row.fileList" type="primary" :href="item.url" target="_black">{{item.name}}</el-link>
                    </template>
                </el-table-column>
                <el-table-column prop="createDate" label="提问时间" width="200" />
                <el-table-column prop="updateDate" label="回答时间" width="200">
                    <template #default="scope">
                        <div>{{scope.row.replyStatus === 1 ? scope.row.updateDate : '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="acceptDeptName" label="受理部门" width="200">
                    <template #default="scope">
                        <div>{{scope.row.acceptDeptName || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="replyStatus" label="问题状态" width="120">
                    <template #default="scope">
                        <span :class="{
                            color_success: scope.row.replyStatus === 1,
                            color_primary: scope.row.replyStatus === 0,
                        }">{{ scope.row.replyStatus === 1 ? "已回答" : "待回答" }}</span>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="200">
                    <template #default="scope">
                        <el-button v-if="permission['回复']" type="text" @click="() => handleEdit(scope.row)">{{scope.row.replyStatus === 1 ? "修改回复" : "回复"}}</el-button>
                        <!-- 公开 -->
                        <el-popconfirm v-if="permission['公开']" :title="`确定${scope.row.isOpen ? '取消公开' : '公开'}吗?`" @confirm="handleOpen(scope.row)">
                            <template #reference>
                                <el-button type="text">{{scope.row.isOpen ? '取消公开' : '公开'}}</el-button>
                            </template>
                        </el-popconfirm>
                        <!-- 删除 -->
                        <el-popconfirm v-if="permission['删除']" :title="`确定删除吗?`" @confirm="handleDelete(scope.row.id)">
                            <template #reference>
                                <el-button class="color_danger" type="text">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>

                <template #empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top: 20px; justify-content: flex-end" background layout="total, sizes, prev, pager, next" :total="totalCount" :default-current-page="1" v-model:current-page="filters.pageNo" v-model:page-size="filters.pageSize"></el-pagination>
            <div v-if="permission['操作日志']" style="text-align: center; margin-top: 10px">
                <el-button type="primary" link @click="logVisible = true">查看操作日志</el-button>
            </div>
        </el-card>

        <EditDrawer title="回复问题" :visible="dialogVisible" @update:visible="dialogVisible = $event" @confirm="handleConfirm">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <div class="form-item">
                    <div class="form-item-label">问题类型</div>
                    <div class="form-item-value">{{form.probTypeName}}</div>
                </div>
                <div class="form-item">
                    <div class="form-item-label">提问时间</div>
                    <div class="form-item-value">{{form.createDate}}</div>
                </div>
                <div v-if="form.replyStatus === 1" class="form-item">
                    <div class="form-item-label">回答时间</div>
                    <div class="form-item-value">{{form.updateDate}}</div>
                </div>
                <div class="form-item">
                    <div class="form-item-label">提问人姓名</div>
                    <div class="form-item-value">{{form.createBy}}</div>
                </div>
                <div class="form-item">
                    <div class="form-item-label">问题标题</div>
                    <div class="form-item-value">{{form.title}}</div>
                </div>
                <div class="form-item">
                    <div class="form-item-label">问题描述</div>
                    <div class="form-item-value">{{form.content}}</div>
                </div>
                <el-form-item label="受理部门" prop="acceptDeptName">
                    <el-input style="width: 400px" v-model="form.acceptDeptName" placeholder="请输入受理部门"></el-input>
                </el-form-item>
                <el-form-item label="回答问题" prop="reply">
                    <el-input type="textarea" :rows="6" style="width: 400px" v-model="form.reply" placeholder="请输入回复内容"></el-input>
                </el-form-item>
            </el-form>
        </EditDrawer>
        
        <LogDialog :visible="logVisible" :path="path" @update:visible="logVisible = $event"></LogDialog>
    </div>
</template>

<style lang="less" scoped>
@import url("../../assets/less/common.less");
@import url("../../assets/less/search.less");

.form-item {
    font-size: 14px;
    line-height: 20px;
    color: @color_regular-text;
    margin-bottom: 20px;
    display: flex;
    .form-item-label {
        flex: none;
        width: 100px;
        text-align: right;
        padding-right: 12px;
        box-sizing: border-box;
    }
    .form-item-value {
        flex: none;
        width: 400px;
        padding-right: 20px;
        color: @color_primary-text;
    }
}
</style>